﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace MergeHosts
{
    public partial class Main : Form
    {
        static string systemHostsFilePath = string.Format(@"{0}\system32\drivers\etc\hosts", Environment.GetEnvironmentVariable("windir"));        

        static List<string> urlOfHosts;

        static List<string> UrlOfHosts 
        {
            get
            {
                if (urlOfHosts == null)
                {
                    urlOfHosts = new List<string>();
                    urlOfHosts.Add("https://smarthosts.googlecode.com/svn/trunk/hosts");
                }
                return urlOfHosts;
            }
        }

        public Main()
        {
            InitializeComponent();
            SetTooltip();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.ShowDialog();
            tbHostsFilePath.Text = fd.FileName;
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            string newHostsFilePaht = tbHostsFilePath.Text.Trim();
            if(string.IsNullOrEmpty(newHostsFilePaht))
            {
                MessageBox.Show("请选择要进行合并的Hosts文件");
                return;
            }

            if(!File.Exists(newHostsFilePaht))
            {
                MessageBox.Show("Hosts文件不存在");
                return;
            }

            MergeData mergeData = new MergeData();
            mergeData.OldFilePath = systemHostsFilePath;
            mergeData.NewFilePath = newHostsFilePaht;

            if(rbtRemain.Checked)
            {
                mergeData.UnincludeOption = "remain";
            }
            else if(rbtRemove.Checked)
            {
                mergeData.UnincludeOption = "remove";
            }
            else { 
                mergeData.UnincludeOption = "remain"; 
            }

            MergeFacade.Merge(mergeData);
            MessageBox.Show("合并完成");
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.yujiankevin.com");
        }

        void SetTooltip()
        {
            ttpDomain.SetToolTip(gbDomain, "如何处理在原Hosts中出现，新Hosts中没有出现的域名解析");            
        }

        private void btnOneClick_Click(object sender, EventArgs e)
        {
            try
            {
                string tempFile = string.Format("{0}/{1}", Environment.CurrentDirectory, Guid.NewGuid());
                string hostsUrl = "https://smarthosts.googlecode.com/svn/trunk/hosts";
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(hostsUrl, tempFile);
                }

                MergeData mergeData = new MergeData();
                mergeData.OldFilePath = systemHostsFilePath;
                mergeData.NewFilePath = tempFile;
                mergeData.UnincludeOption = "remain";
                MergeFacade.Merge(mergeData);
                if(File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show("出现异常，请稍后重试");
            }
        }
    }
}
